using System;
using System.Collections.Generic;
using System.Text;

using DslModeling = global::Microsoft.VisualStudio.Modeling;
using DslDesign = global::Microsoft.VisualStudio.Modeling.Design;
using DslDiagrams = global::Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Evaluant.EussDSL
{
    /// <summary>
    /// Additional behavior for a reference used to complete the reference representation, and to verify its
    /// multiplicity.
    /// </summary>
    public partial class ReferenceNewShape
    {
        public override void OnShapeInserted()
        {
            base.OnShapeInserted();

            using (Transaction t = this.Store.TransactionManager.BeginTransaction("Insert a target new connector"))
            {
                EntityShape targetShape = null;

                foreach (ModelElement obj in this.Partition.ElementDirectory.AllElements)
                {
                    if (obj is EntityShape)
                    {
                        if (((EntityShape)obj).ModelElement is Entity)
                        {
                            if (((Entity)((EntityShape)obj).ModelElement).type == ((ReferenceNew)this.ModelElement).entityChild)
                            {
                                targetShape = (EntityShape)obj;
                            }
                        }
                    }
                }

                if (targetShape != null)
                {
                    bool found = false;

                    foreach (ModelElement obj in this.Partition.ElementDirectory.AllElements)
                    {
                        if (obj is TargetNewConnector)
                        {
                            if (((ReferenceNew)(((TargetNewConnector)obj).FromShape.ModelElement)).entityChild == ((ReferenceNew)this.ModelElement).entityChild
                                && ((Entity)(((TargetNewConnector)obj).ToShape.ModelElement)).type == ((ReferenceNew)this.ModelElement).entityChild
                                && ((ReferenceNew)(((TargetNewConnector)obj).FromShape.ModelElement)).Entity.type == ((ReferenceNew)this.ModelElement).Entity.type )
                            {
                                found = true;
                            }
                        }
                    }


                    if (!found)
                    {
                        TargetNewConnector tncNew = new TargetNewConnector(this.Partition);
                        tncNew.Connect(this, targetShape);
                        this.ParentShape.NestedChildShapes.Add(tncNew);
                        tncNew.Show();
                    }
                }

                MultiplicityVerifier.VerifyReferenceNew(this, null, null);

                if (GlobalData.demandRelocation)
                { // TODO : trouver mieux que cette astuce
                    EntityShape es1 = (EntityShape)(((SourceNewConnector)this.ToRoleLinkShapes[0]).FromShape);
                    EntityShape es2 = targetShape;

                    //PointD newLoc = new PointD((es1.Center.X+es2.Center.X)/2.0, (es1.Center.Y+es2.Center.Y)/2.0);
                    //RectangleD newPos = new RectangleD(newLoc, this.Size);
                    this.Location = new PointD((es1.Location.X + es2.Location.X) / 2.0, (es1.Location.Y + es2.Location.Y) / 2.0);
                    //this.Location.X = ;
                    //this.Location.X = (es1.Location.Y + es2.Location.Y)/2.0;

                    GlobalData.demandRelocation = false;
                }

                if (!GlobalData.isDiagramFilePresent && GlobalData.numberOfEntities > 0)
                {
                    GlobalData.lse.Add(this);
                }

                t.Commit();
            }
        }
    }
}
